﻿using LunarSF.SHomeWorkshop.LunarMarkdownEditor.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Widgets
{
    public class PopupButton : Button
    {
        public PopupButton(string groupName, string buttonText, string args, PythonScriptListItem masterPythonScriptListItem)
        {
            TextOptions.SetTextFormattingMode(this, TextFormattingMode.Display);
            this.FontFamily = Globals.MainWindow.FontFamily;

            Regex regHeader = new Regex(@"\[.*?\]");
            bool showCharsWithSqareBrackets = false;
            var match = regHeader.Match(buttonText);
            string header;
            if (match.Success)
            {
                header = match.Value.Substring(1, match.Value.Length - 2);
                buttonText = header + buttonText.Substring(match.Length);
                showCharsWithSqareBrackets = true;
            }
            else header = "";

            this.Height = 24;
            this.Width = 24;

            if (showCharsWithSqareBrackets)
            {
                this.Content = header;
                if (header.Length >= 2)
                {
                    var txtWidth = Text.MeasureTextWidth(header, 12, "SimSun");
                    this.Width += 26 * (int)(txtWidth / 26) + 26;
                }
            }
            else
            {
                //如果有图标，可以直接使用图标。
                var imgPath = Globals.PathOfWorkspaceScriptImageResourceFolder + buttonText + ".png";
                if (System.IO.File.Exists(imgPath))
                {
                    this.Content = new Image()
                    {
                        Source = Globals.LoadBitmapImageFromFile(imgPath),
                        Height = 16,
                        Width = 16,
                    };
                }
                else
                {
                    this.Content = new Image()
                    {
                        Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/ipscript.png")),
                        Height = 16,
                        Width = 16,
                    };
                }
            }

            this.ToolTip = buttonText;
            this.Padding = new Thickness(0);

            this.groupName = groupName;
            this.args = args;
            this.masterPythonScriptListItem = masterPythonScriptListItem;

            this.Style = Globals.MainWindow.TryFindResource("LSquareButtonStyle") as Style;
            this.Margin = new Thickness(1, 1, 1, 1);
        }

        private string groupName;

        public string GroupName { get { return groupName; } }

        private string buttonText = "S";

        public string ButtonText { get { return buttonText; } }

        private string args;

        public string Args { get { return args; } }

        private PythonScriptListItem masterPythonScriptListItem;

        public PythonScriptListItem MasterPythonScriptListItem { get { return masterPythonScriptListItem; } }
    }
}
